<?php

class S_Controller{
	
	//var $controller = null;
	//var $action = null;

	#public $ndb;
	
	public function __construct(){
		date_default_timezone_set ('Asia/Shanghai');
		#$this->ndb = new DB(MYSQL_CONN_HOST, MYSQL_CONN_USER, MYSQL_CONN_PASSWD, MYSQL_CONN_DB);
	}
	
	/**
	 * url解析
	 */
	public function parseurl(){
		global $config ;
		//URL的普通模式
		if ($config['url_model'] == 1) {
			$control = !empty($_GET['c'])?trim($_GET['c']):'';
			$action = !empty($_GET['a'])?trim($_GET['a']):'';
		} else if($config['url_model'] == 2) {
			if (isset($_SERVER['PATH_INFO'])) {
				$path = trim($_SERVER['PATH_INFO'], '/');
				$paths = explode('/', $path);
				$control = array_shift($paths);
				$action = array_shift($paths);
			}
		}
		$_GET['c'] = $control = !empty($control) ? $control : $config['control'];
		$_GET['a'] = $action = !empty($action) ? $action : $config['action']; 
	}
	
	
	public function run($rosearray = ''){
		$this->parseurl();
		$control = $_GET['c'];
		$action = $_GET['a'];
				
		#if(!rose_check($rosearray, $this->ndb)){
		#	$data['error'] = '您没有权限访问此页面!' ;
        #    $this->loadview('rose_error',$data);
		#	die(-1);
		#}

		$control_file = PHPROOT_PATH . '/controllers/' . $control . '.php';
		if(!file_exists($control_file)) //如果文件不存在提示错误, 否则引入
		{
			$data['error'] = '控制器不存在' . $control_file  ;
			$this->loadview('error',$data);
			exit('');
		}
		include($control_file);

		$class = ucwords($control);	 //将控制器名称中的每个单词首字母大写,来当作控制器的类名
		if(!class_exists($class))    //判断类是否存在, 如果不存在提示错误
		{
			$data['error'] = '未定义的控制器类' . $class  ;
			$this->loadview('error',$data);
			exit('');

		}

		$instance = new $class();	//否则创建实例
		if(!method_exists($instance, $action))	//判断实例$instance中是否存在$action方法, 不存在则提示错误
		{
			$data['error'] = '不存在的方法' . $action ;
			$this->loadview('error',$data);
			exit('');
		}
		$instance->$action();
	}
	
	/**
	 * 加载模型
	 */
	public function &loadmodel($modelname, $param =''){
		$model_file = PHPMODEL_PATH.$modelname.'.php';
		if(!file_exists($model_file)) 
		{ 
			$data['error'] = '模型不存在' . $model_file  ;
			$this->loadview('error',$data);
			exit('');
		}		
		include($model_file);//存在, 则引入
		if(stripos($modelname ,'/')){
			$model = explode('/',$modelname);
			$n = count($model);
			$class = ucwords($model[$n-1]);
		}
		else{
			$class = ucwords($modelname);
		}//获得模型类名
		if(!class_exists($class))
		{
			$data['error'] = '未定义的模型' . $class  ;
			$this->loadview('error',$data);
			exit('');
		} 	
		$model = new $class($param);	//实例化模型类

		return $model;	
	}
	
	/**
	 * 加载视图
	 */
	public function loadview($file , $data=array()){
		$view_file = PHPVIEWS_PATH.$file.'.php';
		if(!empty($data)){
			extract($data);
		}
		if(!file_exists($view_file)) 
		{ 
			$data['error'] = '视图不存在' . $view_file ;
			$this->loadview('error',$data);
			die(-1);
		}
		include($view_file);
	}
	
	/**
	 * 加载lib
	 */
	public function &loadlib($filename, $dir){
		$lib_file = PHPTHIRD_PATH . $dir . '/' . $filename . '.php';
		if(!file_exists($lib_file)) 
		{ 
			exit($filename . '类不存在' . $lib_file);
		}
		include($lib_file);
		$className = ucwords($filename);
		$lib = new $className();
		return $lib;
	}
	
	/**
	 * 加载config
	 */
	public function loadconfig($file, $dir){
		$config_file = PHPETC_PATH.$file.'.php';
		if(!file_exists($config_file)){
			exit('配置文件不存在' . $config_file );
		}
		include($config_file);
		//global $config ;
		return $config ;
	}
}
